package com.jsmile.cloud.tradecenter.web.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.jsmile.cloud.promotioncenter.api.constants.PromotionConstants;
import com.jsmile.cloud.tradecenter.api.constants.TradeConstants;

import lombok.extern.slf4j.Slf4j;

/**
 * rabbitmq消息配置
 *
 * @author 亮亮
 * @version 1.0.0
 * @date 2017年3月30日 下午3:34:16
 */
@Configuration
@Slf4j
public class RabbitmqConfig {

    // TODO 拼团状态变化监听
    @Bean
    public Queue groupDirectQueue() {
        return new Queue(PromotionConstants.PromotionMq.TRADE_GROUP_STATUS_SYNC_QUEUE, true); // true 是否持久
    }

    @Bean
    DirectExchange groupDirectExchange() {
        return new DirectExchange(PromotionConstants.PromotionMq.TRADE_GROUP_STATUS_SYNC_EXCHANGE);
    }

    @Bean
    Binding bindingGroupDirect() {
        return BindingBuilder.bind(groupDirectQueue()).to(groupDirectExchange()).with(PromotionConstants.PromotionMq.TRADE_GROUP_STATUS_SYNC_ROUTE);
    }

    // TODO 订单超时未付款监听
    @Bean
    public Queue overtimeQueue() {
        return new Queue(TradeConstants.TradeMq.ORDER_OVERTIME_QUEUE);
    }

    @Bean
    public DirectExchange overtimeDirectExchange() {
        return new DirectExchange(TradeConstants.TradeMq.ORDER_OVERTIME_EXCHANGE);
    }

    @Bean
    public Binding overtimeBinding() {
        return BindingBuilder.bind(overtimeQueue()).to(overtimeDirectExchange()).with(TradeConstants.TradeMq.ORDER_OVERTIME_ROUTE);
    }

    // TODO 订单超时未付款监听（延时）
    @Bean
    public Queue overtimeTtlQueue() {
        return QueueBuilder.durable(TradeConstants.TradeMq.ORDER_OVERTIME_TTL_QUEUE).withArgument("x-dead-letter-exchange", TradeConstants.TradeMq.ORDER_OVERTIME_EXCHANGE)// 到期后转发的交换机
            .withArgument("x-dead-letter-routing-key", TradeConstants.TradeMq.ORDER_OVERTIME_ROUTE)// 到期后转发的交换机对应的路由key
            .build();
    }

    @Bean
    public DirectExchange overtimeTtlDirectExchange() {
        return new DirectExchange(TradeConstants.TradeMq.ORDER_OVERTIME_TTL_EXCHANGE);
    }

    @Bean
    public Binding overtimeTtlBinding() {
        return BindingBuilder.bind(overtimeTtlQueue()).to(overtimeTtlDirectExchange()).with(TradeConstants.TradeMq.ORDER_OVERTIME_TTL_ROUTE);
    }

}
